package com.hsd.znsh.controller.root;

import com.hsd.znsh.entity.FarmType;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.FarmTypeService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-07 09:12:16
 */
@RequestMapping("/root/farm")
@RestController
public class RootFarmController {

    @Autowired
    private FarmTypeService farmTypeService;

    @GetMapping("/getFarmTypeList")
    public ResultVO getFarmTypeList(Integer size, FarmType farmType){
        farmType.setDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        long totalNum=farmTypeService.getFarmTypeListTotalPages(farmType);
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getFarmTypeList")
    public ResultVO getFarmTypeList(Pageable pageable, FarmType farmType){
        List<FarmType> list=farmTypeService.getFarmTypeList(pageable,farmType);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getFarmTypeInfo")
    public ResultVO getFarmInfo(Integer id){
        FarmType farmType=farmTypeService.getFarmTypeInfo(id);
        return ResultVOUtil.success(farmType);
    }

    @PostMapping("/editFarmType")
    public ResultVO editFarmType(@Valid FarmType farmType, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(farmType.getId()==null){
            farmTypeService.addFarmType(farmType);
            return ResultVOUtil.success("添加成功");
        }else{
            farmTypeService.updateFarmType(farmType);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteFarmType")
    public ResultVO deleteFarmType(Integer id){
        farmTypeService.deleteFarmType(id);
        return ResultVOUtil.success("删除成功");
    }


}
